<%= link_to '编辑', edit_sale_path(@sale) %> |
<%= link_to '返回', sales_path %>

<h3>销售记录详情</h3>

<table class="table table-striped table-bordered">
  <thead>
  <tr>
    <th>编号</th>
    <th>员工</th>
    <th>客户</th>
    <th>产品</th>
    <th>数量</th>
    <th>价格</th>
    <th>销售时间</th>
    <th>产品到期时间</th>
    <th>获得奖励</th>
  </thead>

  <tbody>
      <tr>
        <td><%= @sale.id %></td>
        <td><%= link_to @sale.staff.name, staff_path(@sale.staff) %></td>
        <td><%= link_to @sale.customer.name, customer_path(@sale.customer) %></td>
        <td><%= link_to @sale.product.name, product_path(@sale.product) %></td>
        <td><%= @sale.product_number %></td>
        <td><%= @sale.solid_product.price * @sale.product_number %></td>
        <td><%= @sale.sold_at.strftime('%F') %></td>
        <td><%= @sale.product_expired_at.try(:strftime, '%F') %></td>
        <td><%= @sale.sales_reward %></td>
      </tr>
  </tbody>
</table>

<h4>修改历史</h4>

<table class="table table-striped table-bordered">
  <thead>
  <tr>
    <th>修改时间</th>
    <th>操作人</th>
    <th>操作</th>
    <th>修改说明</th>
    <th>修改内容</th>
  </tr>
  </thead>
  <tbody>
  <% @versions.each do |version| %>
      <tr>
        <td><%= version.created_at.strftime('%F %R') %></td>
        <td><%= link_to User.find(version.whodunnit).name_or_email, staff_path(version.whodunnit) %></td>
        <td><%= t "paper_trail.event.#{version.event}" %></td>
        <td><%= version.explain %></td>
        <td>
          <% version.changeset.slice(:sold_at, :user_id, :product_id, :customer_id, :product_number, :sales_reward).each do |key, value| %>
            <%= t "activerecord.attributes.sale.#{key}" %>:
              <% case key %>
              <% when 'sold_at' %>
                  <%= value[0].try(:in_time_zone).try(:strftime, '%F') %> => <%= value[1].try(:in_time_zone).try(:strftime, '%F') %>,
              <% else %>
                  <%= value[0] %> => <%= value[1] %>,
              <% end %>
          <% end %>
        </td>
      </tr>
  <% end %>
  </tbody>
</table>